package com.example.xuecheng.contentservice.config;

import exception.XueChengException;
import lombok.extern.slf4j.Slf4j;
import model.R;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**   全局异常处理器
 * @author xiehaidong
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(XueChengException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<String> customException(XueChengException e){
        log.error("【系统异常】{}",e.getErrorMessage(),e);
        return R.error(e.getCode(), e.getErrorMessage());

    }
    @ResponseBody
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<String> exception(Exception e){
       log.error("【系统异常】{}",e.getMessage(),e);
       return R.error(500,e.getMessage());

    }
}
